from app import db
from datetime import datetime

class AccountingSubject(db.Model):
    """会计科目表"""
    __tablename__ = 'accounting_subject'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    code = db.Column(db.String(20), unique=True, nullable=False, comment='科目编码')
    name = db.Column(db.String(100), nullable=False)
    parent_id = db.Column(db.BigInteger, nullable=True)
    subject_type = db.Column(db.Enum('ASSET', 'LIABILITY', 'EQUITY', 'INCOME', 'EXPENSE'), nullable=False)
    is_leaf = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    def to_dict(self):
        return {
            'id': self.id,
            'code': self.code,
            'name': self.name,
            'parent_id': self.parent_id,
            'subject_type': self.subject_type,
            'is_leaf': self.is_leaf,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted
        }

class Voucher(db.Model):
    """凭证表"""
    __tablename__ = 'voucher'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    voucher_no = db.Column(db.String(50), unique=True, nullable=False, comment='凭证号')
    period = db.Column(db.Date, nullable=False, comment='会计期间 YYYY-MM')
    summary = db.Column(db.String(200), nullable=False)
    status = db.Column(db.Enum('DRAFT', 'POSTED', 'VOID'), default='DRAFT')
    created_by = db.Column(db.BigInteger, nullable=False)
    posted_at = db.Column(db.DateTime, nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    # 关联分录
    entries = db.relationship('VoucherEntry', backref='voucher', lazy=True, cascade='all, delete-orphan')
    
    def to_dict(self):
        return {
            'id': self.id,
            'voucher_no': self.voucher_no,
            'period': self.period.isoformat() if self.period else None,
            'summary': self.summary,
            'status': self.status,
            'created_by': self.created_by,
            'posted_at': self.posted_at.isoformat() if self.posted_at else None,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted,
            'entries': [entry.to_dict() for entry in self.entries]
        }

class VoucherEntry(db.Model):
    """凭证分录表"""
    __tablename__ = 'voucher_entry'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    voucher_id = db.Column(db.BigInteger, db.ForeignKey('voucher.id'), nullable=False)
    subject_id = db.Column(db.BigInteger, db.ForeignKey('accounting_subject.id'), nullable=False)
    debit_amount = db.Column(db.Numeric(15, 2), default=0.00)
    credit_amount = db.Column(db.Numeric(15, 2), default=0.00)
    currency = db.Column(db.String(3), default='CNY')
    description = db.Column(db.String(200), nullable=True)
    
    # 关联科目
    subject = db.relationship('AccountingSubject', backref='entries', lazy=True)
    
    def to_dict(self):
        return {
            'id': self.id,
            'voucher_id': self.voucher_id,
            'subject_id': self.subject_id,
            'debit_amount': float(self.debit_amount) if self.debit_amount else 0.0,
            'credit_amount': float(self.credit_amount) if self.credit_amount else 0.0,
            'currency': self.currency,
            'description': self.description,
            'subject': self.subject.to_dict() if self.subject else None
        }